<?php
namespace Home\Controller;
use Think\Controller;

class BattleController extends Controller {
    // public function index(){
        
    // }
    //添加对局的信息
    public function add(){
        $battle_info['event_id']     = I('get.event_id');
        $battle_info['event_stage']  = I('get.event_stage');
        $battle_info['sum_frame']    = I('get.sum_frame');
        $battle_info['player1_name'] = I('get.player1_name');
        $battle_info['player2_name'] = I('get.player2_name');
        $battle_info['update_time']  = time();

        if($result = M('Wxapp_battle')->add($battle_info)){
            //添加对局比分
            $battle_score['battle_id']     = $result;
            $battle_score['player1_score'] = 0;
            $battle_score['player2_score'] = 0;
            M('Wxapp_battle_score')->add($battle_score);
            $data['state'] = 1;
            $data['id']    = $result;
            $data['info']  = '添加成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '添加失败！';
        }
        $this->ajaxReturn($data);
    }

    //编辑对局的信息
    public function edit(){
        $where['id']                = I('get.id');
        $battle_info['event_stage']  = I('get.event_stage');
        $battle_info['sum_frame']    = I('get.sum_frame');
        $battle_info['player1_name'] = I('get.player1_name');
        $battle_info['player2_name'] = I('get.player2_name');
        $battle_info['update_time']  = time();
        $result = M('Wxapp_battle')->where($where)->save($battle_info);
        if($result || $result === 0){
            $data['state'] = 1;
            $data['info']  = '编辑成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '编辑失败！';
        }
        $this->ajaxReturn($data);
    }

    //编辑对局的比分信息
    public function edit_score(){
        $battle_id                    = (int)$_GET['battle_id'];
        $battle_info['player1_score'] = (int)$_GET['player1_score'];
        $battle_info['player2_score'] = (int)$_GET['player2_score'];
        $battle_info['update_time']  = time();
        $result = M('Wxapp_battle')->where('id = '.$battle_id)->save($battle_info);
        if($result || $result === 0){
            $data['state'] = 1;
            $data['info']  = '编辑成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '编辑失败！';
        }
         $this->ajaxReturn($data);
    }
    
    //保存登录的code
    public function addLoginCode(){
        $where['id'] = (int)$_GET['id'];
        $login_code  = I('get.login_code');
        if($result = M('Wxapp_battle')->where($where)->save(array('login_code'=>$login_code))){
            $data['state'] = 1;
            $data['info']  = '保存成功！';
        }else{
            $data['state'] = 0;
            $data['info']  = '保存失败！';
        }
        $this->ajaxReturn($data);
    }
}